// 首先引入Mock
const Mock = require('mockjs');

// 设置拦截ajax请求的相应时间
Mock.setup({
    timeout: '200-600'
});

let configArray = [];

// 使用webpack的require.context()遍历所有mock文件
const files = require.context('.', true, /\.js$/);
files.keys().forEach((key) => {
    if (key === './index.js') return;
    configArray = configArray.concat(files(key).default);
});

// 注册所有的mock服务
configArray.forEach((item) => {
    for (let [path, target] of Object.entries(item)) {
        let protocol = path.split('|');
        Mock.mock(new RegExp('^' + protocol[1]), protocol[0], target);
    }
});


//mock 测试登录接口测试，发送post请求
Mock.mock('/users/login', 'post', function (option) {
    console.log('用户登录mock测试...');
    let $name=JSON.parse(option.body).username;
    let $password = JSON.parse(option.body).password;

    if($name==='admin'&& $password==='123456'){
        return Mock.mock({
            code: 200,
            message: '登录成功！'
        })
    }else{
        return Mock.mock({
            code: 400,
            message: '登录失败，请检查用户名或者密码是否正确！'
        })
    }
})